<#import "../common/layout.ftl" as layout/>
<#import "../common/dataTables.ftl" as dataTables/>
<#import "../common/modern-forms.ftl" as mform/>
<#import "../common/modal.ftl" as modal/>


<!DOCTYPE html>
<html>
<@layout.head title="批量变动">
    <@dataTables.head/>
    <@dataTables.buttons_head/>
    <@dataTables.select_head/>
    <@dataTables.fixedHeader_head/>
    <@mform.head/>
    <@select.head/>
</@layout.head>
<body>
<@layout.page>
<div class="main-box">
    <div class="content">
        <div style="position: relative;top:10px; display: flex">
            <div style="width: 500px; position: relative">

                <div>
                    <div style="position: absolute;top: 0px">
                        <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                                onclick="javascript:refreshData();">
                            <i class="fa fa-search"></i> 查询
                        </button>
                        <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                                onclick="javascript:newdetail();">
                            <i class="fa fa-plus"></i> 新增
                        </button>
                        <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                                onclick="javascript:deldetail();">
                            <i class="fa fa-minus"></i> 删除
                        </button>
                    </div>
                </div>
                <@dataTables.commonTable id="mainTable1"/>
            </div>
            <div style="flex: 1;margin-left:20px; position: relative">
                <div>
                    <div style="position: absolute;top: 0px">
                        <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                                onclick="javascript:deldetail2();">
                            <i class="fa fa-minus"></i> 删除变动明细
                        </button>
                        <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                                onclick="javascript:changeStatedetail2();">
                            <i class="fa fa-check"></i> 变动生效
                        </button>
                    </div>
                </div>
                <@dataTables.commonTable id="mainTable2"/>
            </div>
        </div>
    <#--<div id="searchGroup" class="btn-group"-->
    <#--style="position: absolute;top:10px;display: flex;width:calc(100% - 15px);">-->
    <#--<div style=" width: 500px;" id="orgBox">-->

    <#--</div>-->
    <#--</div>-->
    </div>

    <!-- 弹出窗 -->
    <@modal.editModal id="modal-detail" buttonId="btndetailSave" width="800px" height="100px">
        <@mform.container  id="editForm">
            <input id="id" name="id" type="hidden"/>
            <@mform.row>
                <@mform.select2 id='oneCode' name='oneCode' label="资产类型"  size=6>
                    <option value="">请选择资产</option>
                    <#list eqClass as item>
                        <option value="${item.code}">${item.name}</option>
                    </#list>
                </@mform.select2>
                <@mform.select2 id='brandCode' name='brandCode' label="品牌"  size=6>
                    <option value="">请选择品牌</option>
                    <#list brand as item>
                        <option value="${item.code}">${item.name}</option>
                    </#list>
                </@mform.select2>
            </@mform.row>
            <@mform.select2 id='modelId' name='modelId' label="型号名称"  size=12>
                <option value="">请选择型号</option>
            </@mform.select2>
            <@mform.row>
                <@mform.input_text id='reason' name='reason' size=12 label='回收原因' icon='fa-edit' placeholder="请输入"  />
            </@mform.row>
            <div id="warehouseBox">
                <@mform.row>
                    <@mform.select2 id='warehouseId' name='warehouseId' label="选择暂存仓库"  size=12>
                        <#list warehouseList as item>
                            <option value="${item.id?c}">${item.name}</option>
                        </#list>
                    </@mform.select2>
                </@mform.row>
            </div>
        </@mform.container>
    </@modal.editModal>

</@layout.page>
<@layout.foot>
    <@dataTables.foot/>
    <@dataTables.buttons_foot/>
    <@dataTables.select_foot/>
    <@dataTables.fixedHeader_foot/>
    <@mform.foot/>

    <script src="${request.contextPath}/assets/js/jquery.serializejson.min.js?v=3653060703"></script>
    <script type="text/javascript">
        var orgInfo =${organizationName};
        var orgList = [];
        var useTypeList = {
            <#list useType as item>
                "${item.code}": "${item.name}",
            </#list>}
        var state = {
            <#list state as item>
                "${item.code}": "${item.name}",
            </#list>}
        var staffList = {
            <#list staffList as item>
                "${item.id?c}": {
                    name: "${item.name}",
                    organizationId: "${item.organizationId?c}",
                    role: "${item.role}",
                    staffType: "${item.staffType}"
                },
            </#list>}
        var echangeTypeList = {
            <#list echangeType as item>
                "${item.code}": "${item.name}",
            </#list>}
        var warehouseList = {
            <#list warehouseList as item>
                "${item.id?c}": "${item.name}",
            </#list>}

        function putList(orgList, supername, list) {
            if (supername != "") supername = supername + "-";
            for (var i = 0; i < list.length; i++) {
                orgList.push({
                    id: list[i].id,
                    name: list[i].name
                })
                if (list[i].childrenOrg.length > 0) {
                    putList(orgList, supername + list[i].name, list[i].childrenOrg);
                }
            }
        }

        putList(orgList, "", orgInfo.list);

        function getOrgName(id) {
            for (var i = 0; i < orgList.length; i++) {
                if (orgList[i].id == id) return orgList[i].name
            }
        }

        $("#brandCode").bind("change", loadModel);
        $("#oneCode").bind("change", loadModel);

        function loadModel(initValue) {
            $.ajax({
                type: 'POST',
                data: {
                    datas: JSON.stringify({
                        oneCode: $("#oneCode").val(),
                        brandCode: $("#brandCode").val()
                    })
                },
                url: '${request.contextPath}/busi/equipment/use/queryModel',
                success: function (data) {
                    //成功之后
                    $("#modelId").children().remove();
                    var optionDefault = $("<option value='0'>请选择</option>");
                    $("#modelId").append(optionDefault);
                    if (data.success) {
                        $(data.datas).each(function (index, item) {
                            var option = $("<option value='" + item.id + "'>" + item.name + "-" + item.code + "</option>");
                            $("#modelId").append(option);
                        })
                        $("#modelId").val(initValue);
                        $('#select2-model-container').html($("#modelId").find("option:selected").text());
                    }
                    else {
                        swal("错误", data.errorMsg, "error");
                    }
                },
                error: function (xhr, type) {
                    swal("错误", "系统发生内部错误!请稍后再试!", "error");
                }
            })
        }


        var mainTable1 = $('#mainTable1').winningTable({
            columns: [
                {"sWidth": "100px", "title": "设备型号", "data": "name"},
                {"sWidth": "100px", "title": "变动原因", "data": "reason"},
                {"sWidth": "100px", "title": "使用中", "data": "eCount1"},
                {"sWidth": "100px", "title": "已报废", "data": "eCount2"},
                {"sWidth": "50px", "title": "变动时间", "data": "createTime"}
            ],
            pageLength: 10,
            fixedHeader: false,
            searching: true,
            ordering: false,
            "rowCallback": function (row, data, index) {

            }
        });
        var mainTable2 = $('#mainTable2').winningTable({
            columns: [
                {"sWidth": "100px", "title": "机构名称", "data": "organizationId"},
                {"sWidth": "80px", "title": "使用人", "data": "oldStaff"},
                {"sWidth": "120px", "title": "资产名称", "data": "name"},
                {"sWidth": "120px", "title": "资产条形码", "data": "barCode"},
                {"sWidth": "120px", "title": "二维码", "data": "QrCode"},
                {"sWidth": "80px", "title": "领用时间", "data": "buyTime"},
                {"sWidth": "80px", "title": "使用方式", "data": "useType"},
                {"sWidth": "80px", "title": "回收仓库", "data": "warehouseId"},
                {"sWidth": "80px", "title": "变动状态", "data": "state"}
            ],
            pageLength: 10,
            searching: true,
            ordering: false,
            "rowCallback": function (row, data, index) {
                $('td', row).eq(0).html(getOrgName(data.oldOrg||data.organizationId));
                $('td', row).eq(6).html(useTypeList[data.oldUseType||data.useType]);
                if (staffList[data.oldStaff] != null) {
                    data.oldStaff = data.oldStaff;
                    data.oldStaff = staffList[data.oldStaff]["name"]
                    if (data.oldStaff == null) data.oldStaff = ""
                    $('td', row).eq(1).html(data.oldStaff);
                }
                $('td', row).eq(5).html( data.buyTime.substr(0,10));
                $('td', row).eq(7).html(warehouseList[data.warehouseId]);
                $('td', row).eq(8).html(state[data.state]);
            }
        });

        function newdetail() {
            $('#modal-detail').find(".modal-title").text('新增');
            clearForm($('#modal-detail'));
            $("#btndetailSave").unbind();
            $("#btndetailSave").click(savedetail);
            $('#modal-detail').modal();
        }

        function refreshData() {
            var data = {
                datas: JSON.stringify({})
            }
            mainTable1.queryData("${request.contextPath}/busi/equipment/useChange/queryBchangeList", data);
        }

        refreshData();

        function refreshData2() {
            var datas = mainTable1.getSelectedDatas();
            if (datas.length >= 0)
                mainTable2.queryData("${request.contextPath}/busi/equipment/useChange/queryEList", {
                    datas: JSON.stringify({
                        fId: datas[0].id,
                        source: "124004"
                    })
                });
            else
                mainTable2.cleanData();
        }

        $("#mainTable1").on("click", function () {
            refreshData2()
        })

        function savedetail() {
            swal({
                title: "确定保存",
                text: "是否确定保存此数据",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (!result.value)
                    return;
                var requestData = $("#editForm form").serializeJSON();
                if (requestData.modelId == "") {
                    swal("错误", "请选择设备型号", "error");
                    return;
                }
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/busi/equipment/useChange/saveBChange',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                $("#modal-detail").modal('hide');
                                refreshData();
                            });
                        } else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }

        function deldetail() {
            var datas = mainTable1.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var data = datas[0];
            swal({
                title: "确定删除",
                text: "是否确定删除此数据",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (!result.value)
                    return;
                var requestData = {
                    id: data.id
                };
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/busi/equipment/useChange/deleteB',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                refreshData();
                                refreshData2();
                            });
                        } else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }

        function deldetail2() {
            var datas = mainTable2.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var data = datas[0];
            if ("334001".indexOf(datas[0].state) < 0) {
                swal("错误", "当前状态不允许此操作！", "error");
                return;
            }
            swal({
                title: "确定删除",
                text: "是否确定删除此数据",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (!result.value)
                    return;
                var requestData = {
                    id: data.id
                };
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/busi/equipment/useChange/delete',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                refreshData2();
                            });
                        } else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }

        function changeStatedetail2() {
            var datas = mainTable2.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            if ("334001".indexOf(datas[0].state) < 0) {
                swal("错误", "当前状态不允许此操作！", "error");
                return;
            }
            var data = datas[0];
            swal({
                title: "确定生效",
                text: "是否确定此资产变动生效，生效后资产当前信息将发生修改且不可撤销！",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (!result.value)
                    return;
                var requestData = {
                    id: data.id,
                    state: "334002",
                    reason: ""
                };
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/busi/equipment/useChange/checkEChange',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                refreshData2();
                            });
                        } else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }

    </script>
</@layout.foot>
</body>
</html>